{% extends "base.html" %}

{% block title %}统计信息 - 提示词管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <h1 class="mb-4">
            <i class="bi bi-bar-chart"></i> 统计信息
        </h1>
    </div>
</div>

<!-- 基本统计卡片 -->
<div class="row mb-4">
    <div class="col-md-3 mb-3">
        <div class="card stats-card">
            <div class="card-body text-center">
                <i class="bi bi-chat-square-text" style="font-size: 2rem;"></i>
                <h3 class="mt-2">{{ stats.total_prompts }}</h3>
                <p class="mb-0">总提示词数</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card bg-success text-white">
            <div class="card-body text-center">
                <i class="bi bi-check-circle" style="font-size: 2rem;"></i>
                <h3 class="mt-2">{{ stats.enabled_prompts }}</h3>
                <p class="mb-0">已启用</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card bg-secondary text-white">
            <div class="card-body text-center">
                <i class="bi bi-x-circle" style="font-size: 2rem;"></i>
                <h3 class="mt-2">{{ stats.disabled_prompts }}</h3>
                <p class="mb-0">已禁用</p>
            </div>
        </div>
    </div>
    <div class="col-md-3 mb-3">
        <div class="card bg-info text-white">
            <div class="card-body text-center">
                <i class="bi bi-folder" style="font-size: 2rem;"></i>
                <h3 class="mt-2">{{ stats.total_categories }}</h3>
                <p class="mb-0">分类数量</p>
            </div>
        </div>
    </div>
</div>

<!-- 分类统计 -->
<div class="row mb-4">
    <div class="col-lg-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-folder"></i> 分类统计
                </h5>
            </div>
            <div class="card-body">
                {% if stats.category_info %}
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>分类</th>
                                <th>提示词数量</th>
                                <th>状态</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for item in stats.category_info %}
                            <tr>
                                <td>
                                    {% if item.category.color %}
                                        <div class="category-color" style="background-color: {{ item.category.color }};"></div>
                                    {% else %}
                                        <div class="category-color" style="background-color: #6c757d;"></div>
                                    {% endif %}
                                    {{ item.category.name }}
                                </td>
                                <td>
                                    <span class="badge bg-primary">{{ item.count }}</span>
                                </td>
                                <td>
                                    {% if item.category.enabled %}
                                        <span class="badge bg-success">启用</span>
                                    {% else %}
                                        <span class="badge bg-secondary">禁用</span>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <p class="text-muted text-center py-3">暂无分类数据</p>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 标签统计 -->
    <div class="col-lg-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-tags"></i> 标签统计 
                    <span class="badge bg-primary">{{ stats.total_tags }}</span>
                </h5>
            </div>
            <div class="card-body">
                {% if stats.tag_stats %}
                <div style="max-height: 300px; overflow-y: auto;">
                    {% for tag, count in stats.tag_stats.items() %}
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <span class="badge bg-light text-dark">{{ tag }}</span>
                        <span class="badge bg-primary">{{ count }}</span>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <p class="text-muted text-center py-3">暂无标签数据</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 变量统计 -->
<div class="row mb-4">
    <div class="col-lg-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-code-square"></i> 变量统计 
                    <span class="badge bg-primary">{{ stats.total_variables }}</span>
                </h5>
            </div>
            <div class="card-body">
                {% if stats.variable_stats %}
                <div style="max-height: 300px; overflow-y: auto;">
                    {% for variable, count in stats.variable_stats.items() %}
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <code class="variable">{{ variable }}</code>
                        <span class="badge bg-info">{{ count }}</span>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <p class="text-muted text-center py-3">暂无变量数据</p>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 存储统计 -->
    <div class="col-lg-6 mb-4">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-hdd"></i> 存储信息
                </h5>
            </div>
            <div class="card-body">
                {% if stats.storage_stats %}
                <div class="row">
                    <div class="col-6">
                        <div class="text-center">
                            <h4 class="text-primary">{{ stats.storage_stats.prompts_file_size }}</h4>
                            <small class="text-muted">提示词文件大小</small>
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="text-center">
                            <h4 class="text-info">{{ stats.storage_stats.categories_file_size }}</h4>
                            <small class="text-muted">分类文件大小</small>
                        </div>
                    </div>
                </div>
                <hr>
                <div class="text-center">
                    <small class="text-muted">
                        <i class="bi bi-calendar"></i> 
                        最后更新: {{ stats.storage_stats.last_modified or '未知' }}
                    </small>
                </div>
                {% else %}
                <p class="text-muted text-center py-3">暂无存储信息</p>
                {% endif %}
            </div>
        </div>
    </div>
</div>

<!-- 图表区域 -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-pie-chart"></i> 数据概览
                </h5>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-md-3">
                        <div class="border rounded p-3">
                            <h3 class="text-success">{{ "%.1f"|format((stats.enabled_prompts / stats.total_prompts * 100) if stats.total_prompts > 0 else 0) }}%</h3>
                            <small class="text-muted">启用率</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="border rounded p-3">
                            <h3 class="text-info">{{ "%.1f"|format((stats.total_prompts / stats.total_categories) if stats.total_categories > 0 else 0) }}</h3>
                            <small class="text-muted">平均每分类提示词数</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="border rounded p-3">
                            <h3 class="text-warning">{{ stats.total_tags }}</h3>
                            <small class="text-muted">标签总数</small>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="border rounded p-3">
                            <h3 class="text-primary">{{ stats.total_variables }}</h3>
                            <small class="text-muted">变量总数</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
.stats-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}
.border {
    border: 1px solid #dee2e6 !important;
}
</style>
{% endblock %}